﻿using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Controls
{

    public class TreeViewItemViewModel : INotifyPropertyChanged
    {

        public TreeViewItemViewModel(TreeViewItemViewModel parent, bool lazyLoadChildren = false)
        {
            _parent = parent;
            _children = new ObservableCollection<TreeViewItemViewModel>();
            if (lazyLoadChildren)
                _children.Add(DummyChild);
        }


        #region Declare Properties
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (value != name)
                {
                    name = value;
                    NotifyPropertyChanged("name");
                }

            }
        }
        public string Value { get; set; }

        private readonly TreeViewItemViewModel _parent;
        /// <summary>
        /// Returns the parent items of this treenode.
        /// </summary>
        public TreeViewItemViewModel Parent { get => _parent; }

        private readonly ObservableCollection<TreeViewItemViewModel> _children;
        /// <summary>
        /// Returns the child items of this treenode.
        /// </summary>
        public ObservableCollection<TreeViewItemViewModel> Children { get => _children; }


        bool _isExpanded;
        /// <summary>
        /// Gets/sets whether this TreeViewNode is expanded.
        /// </summary>
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                if (value != _isExpanded)
                {
                    _isExpanded = value;
                    NotifyPropertyChanged("IsExpanded");
                }

                // Expand all the way up to the root.
                if (_isExpanded && _parent != null)
                    _parent.IsExpanded = true;

                // Lazy load the child items, if necessary.
                if (HasDummyChild)
                {
                    Children.Remove(DummyChild);
                    LoadChildren();
                }
            }
        }

        bool _isSelected;
        /// <summary>
        /// Gets/sets whether this TreeViewNode is selected.
        /// </summary>
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value != _isSelected)
                {
                    _isSelected = value;
                    NotifyPropertyChanged("IsSelected");
                }
            }
        }


        #region HasChildren 该树节点是否有子节点
        /// <summary>
        /// Returns true if this object's Children have not yet been populated.
        /// </summary>
        public bool HasDummyChild
        {
            get { return Children.Count == 1 && Children[0] == DummyChild; }
        }

        /// <summary>
        /// Returns true if this object's Children have not yet been populated.
        /// </summary>
        public bool HasChildren
        {
            get { return Children != null && Children.Count > 0 && (Children[0] != DummyChild); }
        }
        #endregion

        #region 属性改变时通知view刷新
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        #endregion
        int _testState = 0;
        bool _isEditing = false;
        /// <summary>
        ///节点状态  0-默认  1-试验成功  -1-试验失败 2-试验正在进行中
        /// </summary>
        public int TestState
        {
            get { return _testState; }
            set
            {
                _testState = value;
                NotifyPropertyChanged("TestState");
            }
        }

        public bool IsEditing
        {
            get => _isEditing;
            set
            {
                _isEditing = value;
                NotifyPropertyChanged("IsEditing");
            }
        }


        /// <summary>
        /// Invoked when the child items need to be loaded on demand.
        /// Subclasses can override this to populate the Children collection.
        /// </summary>
        protected virtual void LoadChildren()
        {
        }


        // This is used to create a DummyChild Treeview.
        static readonly TreeViewItemViewModel DummyChild = new TreeViewItemViewModel();
        public TreeViewItemViewModel()
        {
        }


    }
}
